<?php

namespace app\api\controller;

use app\admin\model\Config;
use app\common\controller\ApiBase;
use app\Constant\ApiConstant;
use app\model\DistributionSetting;
use think\facade\Request;
use Wechat\CommerceApp;

class Feed extends ApiBase
{
    /**
     * 视频列表
     * @return \think\response\Json
     */
    public function list()
    {
        $page         = Request::get('page') ?: 1;
        $page_size    = Request::get('page_size') ?: 10;
        $index_status = Request::get('index_status', 0);
        $keyword      = Request::get('keyword');
        $user         = \app\model\User::field('id, level')->where('token', $this->token)->find();
        $level        = $user['level'] ?? 1;

        $query = \app\model\Feed::field('id, talent_appid, export_id, product_id, product_name, product_img_url, product_price, commission, promoter_share_link, feed_token')
            ->where('status', 1);
        if (!empty($index_status)) {
            $query->where('index_status', 1);
        }
        if (!empty($keyword)) {
            $query->where('product_name', 'like', "%{$keyword}%");
        }
        $total = $query->count();

        $lists = $query
            ->withAttr('product_price', function ($value) {
                return $value > 0 ? number_format($value / 100, 2) : "0.00";
            })
            ->withAttr('commission', function ($value, $data) use ($level) {
                if ($value > 0) {
                    //如果用户分销等级是2级，则重新计算佣金
                    $setting = DistributionSetting::getLevelInfo($level, 1);
                    $ratio   = $setting['ratio'] / 100;
                    $price   = round($value * $ratio);
                } else {
                    $price = $value;
                }
                return $price > 0 ? number_format($price / 100, 2) : "0.00";
            })
            ->page($page, $page_size)
            ->order('sort', 'asc')
            ->select();

        $res = [
            'total' => $total,
            'list'  => $lists
        ];
        return $this->callback(ApiConstant::SUCCESS, 'success', $res);
    }

    public function info()
    {
        $id   = Request::get('id');
        $user = \app\model\User::field('id, level, sharer_appid')->where('token', $this->token)->find();
        if (empty($user)) {
            return $this->callback(ApiConstant::NO_LOGIN, '用户未登录');
        }
        $level = $user['level'];

        $info = \app\model\Feed::field('id, talent_appid, export_id, product_id, product_name, product_img_url, product_price, commission')
            ->where('id', $id)
            ->withAttr('product_price', function ($value) {
                return $value > 0 ? number_format($value / 100, 2) : "0.00";
            })
            ->withAttr('commission', function ($value, $data) use ($level) {
                if ($value > 0) {
                    //如果用户分销等级是2级，则重新计算佣金
                    $setting = DistributionSetting::getLevelInfo($level, 1);
                    $ratio   = $setting['ratio'] / 100;
                    $price   = round($value * $ratio);
                } else {
                    $price = $value;
                }
                return $price > 0 ? number_format($price / 100, 2) : "0.00";
            })
            ->find();
        if (empty($info)) {
            return $this->callback(ApiConstant::FAILURE, '没有数据');
        }
        $feed_token = $promoter_share_link = '';
        try {
            $mini_program_appid = Config::where('name', 'wx_xcx_appid')->value('value');
            $feed_info          = CommerceApp::getInstance()->getFeedInfo($info['talent_appid'], $mini_program_appid, $info['export_id'], $user['sharer_appid']);
            if (!empty($feed_info[0])) {
                $feed_info           = $feed_info[0];
                $export_id           = $feed_info['export_id'];
                $feed_token          = $feed_info['feed_token'];
                $promoter_share_link = $feed_info['promoter_share_link'];
                $info['export_id']   = $export_id;
            }
        } catch (\Exception $e) {
        }
        $info['feed_token']          = $feed_token;
        $info['promoter_share_link'] = $promoter_share_link;
        return $this->callback(ApiConstant::SUCCESS, 'success', $info);
    }


}
